package cn.com.backstage.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.*;
import cn.com.api.service.ApiT_memberService;
import cn.com.api.service.ApiT_member_partnerService;
import cn.com.util.*;

/*
 * 合伙人认证控制器
 */
@Controller
@RequestMapping(Utils.SYS_BACKSTAGE + "/")
public class T_memberpartnerController {

	private static Logger logger = Logger.getLogger(T_memberpartnerController.class);

	@Autowired
	private ApiT_memberService apiT_memberService;
	@Autowired
	private ApiT_member_partnerService apiT_member_partnerService;

	/**
	 * 跳转用户合伙人认证信息界面
	 */
	@RequestMapping(value = "member_partner_detail")
	public ModelAndView member_partner_detail(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/member_partner_detail.jsp");
		String userid = request.getParameter("userid");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_id", userid);
		PageInfo page = new PageInfo();
		page.setCurrentPage(1);
		page.setShowCount(5);
		List<ApiT_member_partner> list = apiT_member_partnerService.findT_member_partnersByMap(map, page);
		mv.addObject("partnerdetail", list.get(0));
		return mv;
	}

	/**
	 * 后台审核合伙人认证
	 */
	@RequestMapping(value = "validate_member_partner")
	public void validate_member_partner(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			ApiT_user_admin user = (ApiT_user_admin) request.getSession().getAttribute("user_admin");
			String id = request.getParameter("id");
			String reason = request.getParameter("reason");
			String status = request.getParameter("status");
			ApiT_member_partner apiT_member_partner = new ApiT_member_partner();
			apiT_member_partner.setApply_uid(user.getUser_id());
			apiT_member_partner.setReason(reason);
			apiT_member_partner.setId(Integer.valueOf(id));
			apiT_member_partner.setStatus(Integer.valueOf(status));
			map = apiT_member_partnerService.validate_member_partner(apiT_member_partner);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 后台审核合伙人认证
	 */
	@RequestMapping(value = "recreatePartnerQRcode")
	public void recreatePartnerQRcode(HttpServletRequest request, HttpServletResponse response, String partnerid)
			throws Exception {
		BaseObj baseObj = new BaseObj();
		try {
			ApiT_member_partner apiT_member_partner = apiT_member_partnerService
					.findT_member_partnerById(Integer.valueOf(partnerid));
			ApiT_member apiT_member = apiT_memberService.findT_memberById(apiT_member_partner.getUser_id());
			String partner_code = apiT_member.getPartner_code();
			// 生成合伙人的二维码
			String qrCodeInfo = QRCodeUtils.baseUrl + "/partner/sq-ziliao.html?code=1:" + partner_code;

			String qrcode_img = QRCodeUtils.createQrCode(qrCodeInfo, 300);
			apiT_member_partner.setQrcode_img(qrcode_img);
			apiT_member_partnerService.updateT_member_partner(apiT_member_partner);
			baseObj.setCodeAndInfo(Util.RESULT_RC_SUCCESS, qrcode_img);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			baseObj.handleException(e);
		}
		JsonUtil.printJson(response, JsonUtil.object2json(baseObj));
	}

}
